package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //设置用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断购物车是否存在
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        if (shoppingCartList != null && shoppingCartList.size() == 1) {
            //存在 -> +1
            ShoppingCart shoppingCart1 = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart1.getNumber() + 1);
            shoppingCartMapper.updateShopping(shoppingCart);
        } else {
            //不存在 ->判断是菜品/套餐 -> 添加
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                Dish dishId1 = dishMapper.getDishId(dishId);
                shoppingCart.setName(dishId1.getName());
                shoppingCart.setImage(dishId1.getImage());
                shoppingCart.setAmount(dishId1.getPrice());

            } else {
                Setmeal byId = setmealMapper.getById(shoppingCart.getSetmealId());
                shoppingCart.setName(byId.getName());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setAmount(byId.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.addShoppingCart(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> getShoppingCart() {
        ShoppingCart build = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        return shoppingCartMapper.list(build);
    }

    @Override
    public void deleteShoppingCart() {
        //ShoppingCart build = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();

        shoppingCartMapper.deleteShopping(BaseContext.getCurrentId());
    }

    @Override
    public void deleteShoppingCartById(ShoppingCart shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //获取用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        ShoppingCart shoppingCart2 = list.get(0);
        if (shoppingCart2.getNumber()!=0) {
            shoppingCart.setNumber(shoppingCart2.getNumber() - 1);
            shoppingCartMapper.updateShopping(shoppingCart);
        } else {
            shoppingCartMapper.deleteShoppingCartById(shoppingCart);
        }


    }


}